package com.ruoyi.web.controller.config;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.translate.TransApi;
import com.ruoyi.common.utils.translate.TranslateUtil;
import com.ruoyi.system.domain.config.VsConfig;
import com.ruoyi.system.domain.video.VsLangTranslate;
import com.ruoyi.system.service.config.IVsConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.config.VsDramasConfig;
import com.ruoyi.system.service.config.IVsDramasConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * This file is part of 南阳迈特网络科技有限公司.
 * @company  南阳迈特网络科技有限公司
 * @link     https://www.nymaite.com
 * @document https://product.nymaite.cn
 * @contact  service@nymaite.com
 * @author nymaite
 */
/**
 * 配置Controller
 * 
 * @author zzx
 * @date 2024-05-21
 */
@Api(tags = "首页相关")
@RestController
@RequestMapping("/config/config")
public class VsDramasConfigController extends BaseController
{
    @Autowired
    private IVsDramasConfigService vsDramasConfigService;

    @Autowired
    private IVsConfigService vsConfigMapper;

    /**
     * 查询配置列表
     */
//    @ApiOperation("查询配置列表")
    @GetMapping("/list")
    public TableDataInfo list(VsDramasConfig vsDramasConfig)
    {
        startPage();
        List<VsDramasConfig> list = vsDramasConfigService.selectVsDramasConfigList(vsDramasConfig);
        return getDataTable(list);
    }

    /**
     * 导出配置列表
     */
//    @ApiOperation("导出配置列表")
    @Log(title = "配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VsDramasConfig vsDramasConfig)
    {
        List<VsDramasConfig> list = vsDramasConfigService.selectVsDramasConfigList(vsDramasConfig);
        ExcelUtil<VsDramasConfig> util = new ExcelUtil<VsDramasConfig>(VsDramasConfig.class);
        util.exportExcel(response, list, "配置数据");
    }

    /**
     * 获取配置详细信息
     */
//    @ApiOperation("获取配置详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(vsDramasConfigService.selectVsDramasConfigById(id));
    }

    /**
     * 新增配置
     */
//    @ApiOperation("新增配置")
    @Log(title = "配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VsDramasConfig vsDramasConfig)
    {
        return toAjax(vsDramasConfigService.insertVsDramasConfig(vsDramasConfig));
    }

    /**
     * 修改配置
     */
//    @ApiOperation("修改配置")
    @Log(title = "配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VsDramasConfig vsDramasConfig)
    {
        return toAjax(vsDramasConfigService.updateVsDramasConfig(vsDramasConfig));
    }

    /**
     * 删除配置
     */
//    @ApiOperation("删除配置")
    @Log(title = "配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(vsDramasConfigService.deleteVsDramasConfigByIds(ids));
    }


    /**
     * 手机端初始化
     */
    @ApiOperation("获取配置信息")
    @PostMapping( "init")
    public AjaxResult phoneInit(HttpServletRequest request, @RequestBody VsDramasConfig vsDramasConfig)
    {
        if (StringUtils.isEmpty(request.getHeader("Lang-Id"))) {
            vsDramasConfig.setLangId("65");
        } else {
            String lang = request.getHeader("Lang-Id");
            vsDramasConfig.setLangId(lang);
        }
        return success(vsDramasConfigService.phoneInit(vsDramasConfig));
    }

    @PostMapping("/withdrawRule")
    public AjaxResult withdrawRule(HttpServletRequest request, @RequestBody VsDramasConfig vsDramasConfig) {
        if (getLoginUser().getVsUser() == null) {
            return error("暂未登录！");
        }
        List<VsDramasConfig> list = vsDramasConfigService.selectVsDramasConfigList(vsDramasConfig);
        if (list.size() == 0) {
            return error("未查询到提现规则！");
        }
        String lang = "zh";
        if (!StringUtils.isEmpty(request.getHeader("lang"))) {
            lang = request.getHeader("lang");
        }
        JSONObject jsonObject = JSONObject.parseObject(list.get(0).getValue());
        // 获取methods数组
        JSONArray methods = jsonObject.getJSONArray("methods");
        JSONArray newMethods = new JSONArray();
        // 循环遍历methods数组中的元素
        for (int i = 0; i < methods.size(); i++) {
            String method = "";
            if ("bank".equals(methods.getString(i))) {
                method = "银行卡";
            } else if ("alipay".equals(methods.getString(i))) {
                method = "支付宝";
            } else if ("wechat".equals(methods.getString(i))) {
                method = "微信";
            }
            String s = batchTranslate(method, lang);
            JSONObject newMethod = new JSONObject();
            newMethod.put("name", s);
            newMethod.put("type", methods.getString(i));
            newMethods.add(newMethod);
        }
        // 替换原有JSON对象中的methods数组
        jsonObject.put("methods", newMethods);
        return success(jsonObject);
    }

    private String batchTranslate(String s, String languageType) {
        VsConfig config = new VsConfig();
        config.setGroup("basic");
        List<VsConfig> configList = vsConfigMapper.selectVsConfigList(config);
        String appid = null;
        String sec_key = null;
        for (VsConfig v : configList) {
            if ("app_id".equals(v.getName())) {
                appid = v.getValue();
            }
            if ("sec_key".equals(v.getName())) {
                sec_key = v.getValue();
            }
        }
        TransApi api = new TransApi(appid,sec_key);
        TranslateUtil util = new TranslateUtil();
        String result = api.getTransResult(s, "auto", languageType);
        return util.conversion(result);
    }

}
